package com.app.couponsharelife.ui.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.app.couponsharelife.R
import com.app.couponsharelife.data.bean.LifeShopPageBean
import com.app.couponsharelife.data.bean.PageTipBean
import com.app.couponsharelife.databinding.LocalShopListItemBinding
import com.app.couponsharelife.manage.Contacts.APP_LIFE_IMGURL
import com.app.couponsharelife.ui.listener.OnClickListener

class LocalShopsListAdapter(val list: MutableList<Any>,
                            private val listener: OnClickListener) :
        androidx.recyclerview.widget.RecyclerView.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>() {

    fun setData(data: List<Any>, page: Int) {
        if (page == 2) list.clear()
        list.addAll(data)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        return when (viewType) {
            0 -> ViewHolderTip(DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context), R.layout.page_list_tip_item, parent, false))
            else -> ViewHolder(DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context), R.layout.local_shop_list_item, parent, false))
    }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun getItemViewType(position: Int): Int {
        return if (list[position] is PageTipBean) 0 else 1
    }

    override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
        if (getItemViewType(position) == 0) {
            (holder as ViewHolderTip).databinding.pageTipBean = list[position] as PageTipBean
        } else {
            val lifeShopPageBean = list[position] as LifeShopPageBean.RecordsBean
            var image = if (lifeShopPageBean.listImages.isNotEmpty()) lifeShopPageBean.listImages[0] else ""
            if (image.contains("q11")){
                image= "http:$image"
            }else{
                image=APP_LIFE_IMGURL+image
            }
            Glide.with((holder as ViewHolder).itemView).asBitmap()
                    .apply(RequestOptions().centerCrop().placeholder(R.drawable.ic_app_place_holder3))
                    .load(image)
                    .into(holder.databinding.ivLocalShop)
            holder.databinding.tvLocalShop.text = lifeShopPageBean.shopName
            holder.databinding.tvShopTag.text = if (lifeShopPageBean.className != null) lifeShopPageBean.className else ""
            holder.databinding.tvCouponRetainNum.text = String.format("卡券总量：%s张", lifeShopPageBean.cardNum)
            holder.databinding.tvCouponGetPeopleNum.text = String.format("已领人数：%s人", lifeShopPageBean.number)
            if (lifeShopPageBean.couponOneContent != "") {
                holder.databinding.tvCoupon.text = lifeShopPageBean.couponOneContent
                holder.databinding.clCouponContainer.visibility = View.VISIBLE
            } else {
                holder.databinding.clCouponContainer.visibility = View.GONE
            }
            if (lifeShopPageBean.couponTwoContent != "") {
                holder.databinding.tvDiscount.text = lifeShopPageBean.couponTwoContent
                holder.databinding.clDiscountContainer.visibility = View.VISIBLE
            } else {
                holder.databinding.clDiscountContainer.visibility = View.GONE
            }
            holder.databinding.tvShopDistance.text = lifeShopPageBean.distance
        }
    }

    inner class ViewHolder(val databinding: LocalShopListItemBinding) : androidx.recyclerview.widget.RecyclerView.ViewHolder(databinding.root) {
        init {
            databinding.root.setOnClickListener {
                if (adapterPosition != -1) {
                    listener.click(0, list[adapterPosition])
                }
            }
        }
    }
}